package com.xinlu.hljssc.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.xinlu.common.game.bo.TzjlBo;
import com.xinlu.common.game.bo.WcSjglBo;
import com.xinlu.common.game.bo.WcZjxxBo;
import com.xinlu.common.game.value.WcSjglVo;
import com.xinlu.common.log.Logger;
import com.xinlu.cqssc.bo.CqsscBo;
import com.xinlu.cqssc.form.CqsscForm;
import com.xinlu.derek.common.BaseAction;
import com.xinlu.derek.common.BaseForm;
import com.xinlu.hljssc.form.HljsscForm;
import com.xinlu.jxssc.form.JxsscForm;
import com.xinlu.persistence.Users;
import com.xinlu.util.tool.CalcZhiheUtils;

public class HljsscAction extends BaseAction {

	Logger logger = Logger.getLogger(HljsscAction.class);
	
	private HljsscForm fr = new HljsscForm();
	
	@Override
	public BaseForm getModel() {
		
		return fr;
	}
	
	/************	Bo 注入	******************/
	
	private WcSjglBo wcSjglBo;
	
	public void setWcSjglBo(WcSjglBo wcSjglBo) {
		this.wcSjglBo = wcSjglBo;
	}
	
	private CqsscBo cqsscBo;
	
	public void setCqsscBo(CqsscBo cqsscBo) {
		this.cqsscBo = cqsscBo;
	}
	
	private WcZjxxBo wcZjxxBo;
	
	public void setWcZjxxBo(WcZjxxBo wcZjxxBo) {
		this.wcZjxxBo = wcZjxxBo;
	}
	
	private TzjlBo tzjlBo;
	
	public void setTzjlBo(TzjlBo tzjlBo) {
		this.tzjlBo = tzjlBo;
	}

	@Override
	public String doExecute() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String addTzjlInfo() throws IOException{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpSession session = request.getSession();
		
		Users user = (Users) session.getAttribute("userinfo");
		
//		fr.getAllmoney() fr.getOnemoney() fr.getBeishu() fr.getCodes() fr.getExpect() fr.getIstop() fr.getZhuihaobeishu() fr.getZhuihaoqihao() fr.getXl() fr.getYjf() fr.getProtype() fr.getIszhuihao()
		double allmoney = user.getAllmoney();
		
		user = this.cqsscBo.saveTzjlInfo(fr.getAllmoney(), fr.getOnemoney(), fr.getBeishu(), fr.getProtype(), fr.getCodes(), fr.getExpect(), user.getUsername(), fr.getXl(), fr.getYjf(), "WC", fr.getZhuihaobeishu(), fr.getZhuihaoqihao(), fr.getIszhuihao(), fr.getIstop(), user, "2007/04/25");
		
		if (allmoney ==  user.getAllmoney()) {
			
			response.getWriter().write("error");
		}else{
			
			session.setAttribute("userinfo", user);
			try {
				response.getWriter().write(user.getUermoney()+"");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		return null;
	}
	
	/**
	 * 
	 * Function :	获取今日列表
	 * @return
	 * @throws IOException
	 * Derek
	 * 2011-5-20 下午09:24:26
	 */
	public String getLottyList() throws IOException{
		
		HttpServletResponse response = ServletActionContext.getResponse();
		
		List list = this.wcSjglBo.getLottyList("2007/04/25", "HLJSSC");
		
		JSONArray jsonArray = JSONArray.fromObject(list);
		
		response.setCharacterEncoding("UTF-8");
		
		response.getWriter().write(jsonArray.toString());
		
		return null;
	}
	
	
	public String checkAllMoney() throws IOException{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpSession session = request.getSession();
		response.setCharacterEncoding("UTF-8");
		
		
		Users user = (Users) session.getAttribute("userinfo");
		if(user != null){
			
			if (fr.getAllmoney() > user.getUermoney()) {
				
				response.getWriter().write("1");
			}else{
				
				response.getWriter().write("0");
			}
		}else{
			
			response.getWriter().write("2");
		}
		
		return null;
	}
	
	/**
	 * 
	 * Function :	获取当前期数及截止时间
	 * Derek
	 * 2011-5-21 下午03:29:14
	 * @throws IOException 
	 */
	public String getCqsscEndNow() throws IOException{
		HttpServletResponse response = ServletActionContext.getResponse();
		
		response.setCharacterEncoding("UTF-8");
		
		WcSjglVo vo =  wcSjglBo.getNewQishuAndJiezhishijian("2007/04/25", "HLJSSC");
		
		if (vo != null) {
			JSONObject jsonObject =  JSONObject.fromObject(vo);
			
			response.getWriter().write(jsonObject.toString());
		}
		
		return null;
	}
	
	/**
	 * 
	 * Function :		获取组选和值
	 * @return
	 * @throws IOException
	 * Derek
	 * 2011-5-23 下午07:32:47
	 */
	public String getZhixuanhezhiVal() throws IOException{
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		
		String val = CalcZhiheUtils.calcZhihe();
		JSONObject jsonObject = new JSONObject();
		
		jsonObject.put("zxhzVal", val);
		
		response.getWriter().write(jsonObject.toString());
		
		return null;
	}
	
	public String getZjxxList() throws IOException{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		
		List zjxxList = this.wcZjxxBo.getAllZjxxList("HLJSSC");
		
		if (zjxxList != null && zjxxList.size() > 0 ) {
			
			JSONArray jsonArray = JSONArray.fromObject(zjxxList);
			
			response.getWriter().write(jsonArray.toString());
		}else{
			
			response.getWriter().write("");
		}
		
		return null;
	}
	
	/**
	 * 
	 * Function :		获取今日投注
	 * @return
	 * @throws IOException
	 * Derek
	 * 2011-6-10 下午10:49:39
	 */
	public String getJrtzList() throws IOException{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpSession session = request.getSession();
		
		Users users = (Users)session.getAttribute("userinfo");
		
		if (users != null) {
			
			List list = this.tzjlBo.getJrtzList("HLJSSC%", users.getUsername());
			
			JSONArray jsonArray = JSONArray.fromObject(list);
			response.setCharacterEncoding("UTF-8");
			
			response.getWriter().write(jsonArray.toString());
			
		}
		
		return null;
		
	}

	@Override
	public void formInit() {
		
		logger.startMethod("formInit");
		
		logger.endMethod("formInit");
	}

	
	
	
}
